from flask import Flask,g,render_template, request,jsonify
from flask_cors import CORS
import model_sql as pgsql


#跨域设置
app=Flask(__name__)
CORS(app, resources=r'/*')	# 注册CORS, "/*" 允许访问所有api

# 设置路由，装饰器绑定触发函数 login登录
@app.route("/",methods=['POST'])
def user_login():
    username = request.get_json()['username']
    password=request.get_json()['password']
    print(username,password)
    # return True or False
    return pgsql.check_user(username, password)

# 设置路由，装饰器绑定触发函数 login注册
@app.route("/register",methods=['POST'])
def user_register():
    username = request.get_json()['userName']
    password=request.get_json()['password']
    emai=request.get_json()['uerEmail']
    print(username,password,emai)
    # return True or False
    test=pgsql.register_user(username, emai,password)
    print(test)
    return test

# 读取共享单车轨迹点 gj20201221
@app.route('/bikeTrace',methods=['POST'])
def get_gx_gj():
    gjTable=request.get_json()['date']
    print(gjTable)
    return pgsql.get_bikeTace(gjTable)

# 读取厦门路网数据 xm_osm_road
@app.route('/xmRoad',methods=['GET'])
def get_xmRoad():
    return pgsql.get_xmRoad()

# 读取厦门路网数据 xm_osm_road
@app.route('/xmRoadCount',methods=['GET','POST'])
def get_xmRoadCount():
    return pgsql.get_xmRoad_trafficCounts()

# 读取厦门绿道数据 xm_osm_road
@app.route('/greenTrace',methods=['GET','POST'])
def get_xmRoadCount_tj():
    count=request.get_json()['trCount']
    print(count)
    return pgsql.get_xmRoad_trafficCounts_tiaojian(count)

# 统计厦门绿道数据
@app.route('/countGreen',methods=['GET','POST'])
def count_xmGreen():
    count=request.get_json()['trCount']
    return pgsql.count_xmRoad_trafficCounts_tiaojian(count)
# 读取厦门共享单车停车点
@app.route('/xmParkPoint')
def get_xmParkPoint():
    return pgsql.get_bikeParkPoint()

#读取泰森多边形区域
@app.route('/tession')
def get_xmTession():
    return pgsql.get_tession()


# 读取厦门共享单车OD点
@app.route('/odpoint',methods=['POST'])
def get_xmODPoint():
    odTable = request.get_json()['dateAndClass']
    print(odTable)
    return pgsql.get_ODPoint(odTable)

# 读取厦门街景图片
@app.route('/getphoto',methods=['POST','GET'])
def get_photo():
    pointx=request.get_json()['x']
    pointy=request.get_json()['y']
    print(pointx,pointy)
    return pgsql.get_streetphoto(pointx,pointy)

#读取第一层调度区域
@app.route('/firstPatchArea')
def get_dispatcharea():
    return pgsql.get_dispatchArea()

#读取第一层调度路线
@app.route('/firstDisLine')
def get_dispatchline():
    return pgsql.get_dispatchLine()

#读取第一次调度区域站点
@app.route('/firstPatchPoint')
def get_dispatchpoint():
    return pgsql.get_firDispatchPoint()

#读取第二层调度区域
@app.route('/secondDispactArea',methods=['POST','GET'])
def get_secondDispatch():
    id=request.get_json()['areaID']
    return pgsql.get_secDispatchArea(id)

#读取第二层调取区域路线
@app.route('/secondDisLine',methods=['POST','GET'])
def get_secondDispatchLine():
    id=request.get_json()['areaID']
    return pgsql.get_secDispatchLine(id)

# 读取第二层调取区域站点
@app.route('/secondDisPoint',methods=['POST','GET'])
def get_secondDispatchPoint():
    id=request.get_json()['areaID']
    return pgsql.get_secDispatchPoint(id)

if __name__ == '__main__':

   app.run(debug=True)